from zeus.operation_service.app.core.alarm_util.alarm_struct import AlarmInfo
from zeus.operation_service.app.core.alarm_util.data_util import get_event_data
from zeus.operation_service.app.proxy.host import HostProxy


def get_range_alarm_info(start_time, end_time):
    """
    返回时间段内事件上报
    """
    event_list = get_event_data(start_time, end_time)
    res_data = list()
    # 用于判断事件是否上报过
    event_flag = dict()
    host_map = dict()
    len_event = len(event_list)
    for i in range(len_event - 1, -1, -1):
        event_id = event_list[i]['Attributes']['entity_id']
        event_metric = event_list[i]['Resource']['metric']
        if event_flag.get(event_id) and event_flag.get(event_id) == event_metric:
            continue
        machine_id = event_list[i]["Resource"]["labels"]["Host"]
        if not host_map.get(machine_id):
            # "d0ad81cc-9585-41ad-87ee-83d78837a694-10.90.183.80"
            host_ip = machine_id.split('-')[-1]
            host = HostProxy().get_host_by_ip(host_ip)
            host_map[machine_id] = host.get('host_name', '-')
        data = AlarmInfo(event_list[i], host_map)
        event_flag[event_id] = event_metric
        res_data.append(data.__dict__)
    return res_data

